<?php


/**
 * Presenter pro spravu nastaveni webove prezentace.
 *
 * @author David Dusek
 */
use Nette\Application\UI\Form;

class PageConfigPresenter extends AdminPresenter
{
    public function renderDefault() {
        $this->isAllowed("config", TRUE);
        $this->template->title = $this->getTitle("title", "settings");        
    }
    
    /**
     * Formular pro editaci vychoziho nastaveni stranek.
     * @return \Nette\Application\UI\Form 
     */
    protected function createComponentPageConfigForm() {
        $form = new Form();
        
        $layoutsOptions = $this->core->runHook("hookGetLayouts", TRUE, 'ConfigModule');
        $stylesheetOptions = $this->core->runHook("hookGetStylesheets", array("/css"), 'ConfigModule');
                        
        $form->addText("title", $this->translate('pageTitle'));
        $form->addTextArea("description", $this->translate('description'));
        $form->addTextArea("keywords", $this->translate('keywords'));
        $form->addSelect("layout", $this->translate('layout'), $layoutsOptions);
        $form->addSelect("style", $this->translate('style'), $stylesheetOptions);
        $form->addText("icon", $this->translate('icon'));
        
        $config = $pcRepository = $this->em->getRepository('\CMS\Page\Models\PageConfig')->getConfig(TRUE);
        $form->setDefaults($config);
        
        $form->addSubmit('edit', $this->translate("edit"));
        $form->onSuccess[] = callback($this, 'pageConfigFormSubmitted');
        return $form;
    }
    
    public function pageConfigFormSubmitted(Form $form) {
        $values = $form->getValues();
                
        $config = $this->em->getRepository('\CMS\Page\Models\PageConfig')->getConfig();        
        $config->setTitle($values["title"]);
        $config->setDescription($values["description"]);
        $config->setKeywords($values["keywords"]);
        $config->setLayout($values["layout"]);
        $config->setStyle($values["style"]);
        $config->setIcon($values["icon"]);
        $this->em->persist($config);
        $this->em->flush();
        
        $this->flashMessage($this->translate("editConfigSucceeded"));
        $this->redirect("this");
    }
}
